package javacv;

import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

/**
 * @author ilongsay
 * @date: Create in 2020-03-12 11:17
 * @description: don't bb,show you code
 */
public class StartVideoBatServiceImpl{
    public boolean execNodeJsBat(String httpPort, String wsPort, String secret) {
        String[] cmds = new String[3];
        String jsPath = "/vedio/websocket-relay.js";
        String appPath = System.getProperty("user.dir");

        String absolutePath = appPath + jsPath;

        try {
            StringBuilder cmdSql = new StringBuilder("node ");
            cmdSql.append("D:\\myapp\\testrczs\\src\\main\\resources\\vedio\\websocket-relay.js").append(" ");
            cmdSql.append(secret).append(" ");
            cmdSql.append(httpPort).append(" ");
            cmdSql.append(wsPort);
            cmds[0] = "cmd";
            cmds[1] = "/c";
            cmds[2] = cmdSql.toString();
            Process exec = Runtime.getRuntime().exec(cmds);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public void execFfBat() throws IOException {
        String[] cmds = new String[3];
        cmds[0] = "cmd";
        cmds[1] = "/c";

        StringBuilder cmdSql = new StringBuilder("ffmpeg ");
        cmdSql.append("-rtsp_transport").append(" ");
        cmdSql.append("tcp").append(" ");
        cmdSql.append("-i").append(" ");
        cmdSql.append("\"rtsp://192.168.3.133:554/user=admin&password=&channel=1&stream=0\"").append(" ");
        cmdSql.append("-r").append(" ");
        cmdSql.append("28 ").append("-q ").append("0 ").append("-f ").append("mpegts ").append("-codec:v ").append("mpeg1video ");
        cmdSql.append("-s ").append("1280x720 ").append("http://127.0.0.1:8089/123456");
        cmds[2] = cmdSql.toString();
        Process exec = Runtime.getRuntime().exec(cmds);
    }

    public boolean execFFmpegBat(String rtspUrl,String httpUrl, String secret) {
        String[] cmds = new String[3];
        StringBuilder stringBuilder = new StringBuilder("ffmpeg ");
        stringBuilder.append("-i").append(" ");
        stringBuilder.append(rtspUrl).append(" ");
        stringBuilder.append("-rtsp_transport").append(" ");
        stringBuilder.append("tcp").append(" ");
        stringBuilder.append("-q").append(" ");
        stringBuilder.append("0").append(" ");
        stringBuilder.append("-f").append(" ");
        stringBuilder.append("mpegts").append(" ");
        stringBuilder.append("-codec:v").append(" ");
        stringBuilder.append("mpeg1video").append(" ");
        stringBuilder.append("-s").append(" ");
        stringBuilder.append("1280x720").append(" ");
        stringBuilder.append(httpUrl +"/" +secret);
        cmds[0] = "cmd";
        cmds[1] = "/c";
        cmds[2] = stringBuilder.toString();
        try {
            Process exec = Runtime.getRuntime().exec(cmds);

            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Test
    public void test() {
        String property = System.getProperty("user.dir");
        System.out.println(property);

    }

    public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {
        StartVideoBatServiceImpl service = new StartVideoBatServiceImpl();
//        boolean b=service.execNodeJsBat("8089", "8098", "123456");
//        if (b) {
//            service.execFFmpegBat("rtsp://192.168.3.111/vga1","http://127.0.0.1:8089","123456");
//        }

        service.execNodeJsBat("8089", "8098", "123456");

        Thread.sleep(3000);
        service.execFfBat();

    }
}
